
import { WeaDialog, WeaLocaleProvider } from 'ecCom';
import { Button, message } from 'antd';
import isFunction from 'lodash/isFunction';
import QuoteItem from './QuoteItem';
import CommunicationRichText from './CommunicationRichText';
const getLabel = WeaLocaleProvider.getLabel;
export default class ReplyDialog extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      visible: true,
      replyInfo: {
        signReplyData: this.props.replyInfo || {},
        signReplyFlag: true,
        signReplyStr: this.props.replyRemarkStr || ''
      },
    }
  }

  componentWillReceiveProps(nextProps) {
    const { replyInfo, replyRemarkStr } = nextProps;
    this.setState({
      visible: true,
      replyInfo: {
        signReplyData: replyInfo,
        signReplyFlag: true,
        signReplyStr: replyRemarkStr
      }
    })
  }
  onCancel = () => {
    let resetObj = {
      replyInfo: {
        signReplyData:{},
        signReplyFlag: false,
        replyRemarkStr: ''
      },
    };
    this.setState( { 
      visible: false, 
      ...resetObj
    });
  }
  submit = () => {
      let  { store: {saveReplyOthers}, replyInfo, submitCallBackFun } = this.props;
      let {
        richValue,
        anonymous,
        relatedaccList4E9= [],
        relatedcusList4E9= [],
        relateddocList4E9= [],
        relatedprjList4E9= [],
        relatedwfList4E9= [],
        relatemutilprjs4E9= []} =  this.refs.replyRich.state;
        //获取富文本输入值
      richValue =  this.refs.replyRich.refs.rich.getEditor().getData();
      richValue = richValue.replace(/​/ig, '');
      let str = richValue;
      if(richValue != '' && richValue.indexOf('<img') < 0) {
        str = richValue.replace(/<\/?[^>]*>/g, '');
        str = str.replace(/[\u200B-\u200D\uFEFF]/g, "");
        str = str.replace(/\n/g,'');
        str = str.replace("&#8203;", "").replace(/&nbsp;/g, '');
        str = str.trim();
      } 
      if (!str) {
        message.error(getLabel(382648, '请输入交流内容'));
        return false;
      }
      let richParams = {remark: richValue, isAnonymous: anonymous};
      richParams.relatedprj = relatedprjList4E9.map((obj) => obj.id).join();
      richParams.relatedcus = relatedcusList4E9.map((obj) => obj.id).join();
      richParams.relatedwf = relatedwfList4E9.map((obj) => obj.id).join();
      richParams.relateddoc = relateddocList4E9.map((obj) => obj.id).join();
      richParams.projectIDs = relatemutilprjs4E9.map((obj) => obj.id).join();
      richParams.relatedacc = relatedaccList4E9.map((obj) => obj.fileid || obj.id).join();
      saveReplyOthers({replyInfo, richParams}, submitCallBackFun);
      if(richValue != '') {
        this.onCancel();
      }
  }

  render() {
    const { visible, replyInfo } = this.state;
    const { store } = this.props;
    const { appStatus } = store;
    let buttons = [
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@uxegtk@confirm`} type={'primary'} onClick={() => {this.submit()}}>
        {getLabel(83446, '确定')}
      </Button>,
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@33xp5x@close`} type={'ghost'} onClick={() => {this.onCancel()}}>
        {getLabel('309', '关闭')}
      </Button> 
    ]
    return (
      <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@6hdv7p`}
            title={getLabel(385534, "回复") + ': ' + replyInfo.signReplyData.displayname}
            className="reply-dialog"
            visible={visible}
            buttons={buttons}
            onCancel={() => {this.onCancel()}}
            icon={'icon-coms-workflow'}
            iconBgcolor={'rgb(0,121,222)'}
            style={{width: 1000}}
            draggable={true}
          >
            <div className="cowork-reply-remark">
              <QuoteItem ecId={`${this && this.props && this.props.ecId || ''}_QuoteItem@ouklk1`} 
                signReplyInfo={replyInfo} 
                store={store} 
                isFromLoglist={true}
              />
              <CommunicationRichText ecId={`${this && this.props && this.props.ecId || ''}_CommunicationRichText@2z50xt`} 
                editorId={'replyRichremark'}
                ref ={'replyRich'}
                appStatus={{}} 
                submit={this.submit} 
                cancel={this.onCancel}
                noNeedSubmitBtn={true}
                noNeedCancelBtn={true}
                appStatus={appStatus}
                logParams={store.logItemBaseParams}
              />
            </div>
          </WeaDialog>
    )
  }
}